//
//  CYUserCarVC.m
//  CreditAddressBook
//
//  Created by LE on 15/12/17.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYUserCarVC.h"
#import "MDTabBarViewController.h"
#import "CYUserCarDropCarVC.h"
#import "CYUserCarLeaseVC.h"
#import "LEBarButtonItem.h"
#import "CYUserCarPublishTableVC.h"
#import "UIViewController+Category.h"
#import "CYJumpsTool.h"
#import "CYPersonalDetailsVController.h"
#import "CYDiscoveryHttpTool.h"
#import "CYMeHttpTool.h"
#import "CYCompanyUserTool.h"
#import "MBProgressHUD+MJ.h"
#import "CYLoginHttpTool.h"
#import "PaperButton.h"
#import "LEMoreView.h"

@interface CYUserCarVC ()<MDTabBarViewControllerDelegate>

@property(nonatomic, weak) MDTabBarViewController *tabBarViewController;
@property(nonatomic, strong) CYUserCarLeaseVC *leaseVC;
@property(nonatomic, strong) CYUserCarDropCarVC *dropCarVC;
@end

@implementation CYUserCarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的租车信息";
    self.view.backgroundColor = [UIColor whiteColor];
    [self addBarButton];
    [self loadUserStatus];
}

- (void)initTabBar {
    MDTabBarViewController *tabBarViewController = [[MDTabBarViewController alloc] initWithDelegate:self];
    self.tabBarViewController = tabBarViewController;
    
    tabBarViewController.tabBar.textColor = CYNaviThemeColor;
    tabBarViewController.tabBar.normalTextColor = [UIColor grayColor];
    tabBarViewController.tabBar.backgroundColor = [UIColor whiteColor];
    tabBarViewController.tabBar.indicatorColor = CYNaviThemeColor;
    tabBarViewController.tabBar.normalTextFont = [UIFont systemFontOfSize:12];
//    tabBarViewController.tabBar.rippleColor = CYNaviThemeColor;
    
    NSArray *names = @[@"租赁中", @"已还车"];
    [tabBarViewController setItems:names];
    self.leaseVC = [[CYUserCarLeaseVC alloc] init];
    self.leaseVC.userType = self.userType;
    self.dropCarVC = [[CYUserCarDropCarVC alloc] init];
    self.dropCarVC.userType = self.userType;
    
    [self addChildViewController:tabBarViewController];
    [self.view addSubview:tabBarViewController.view];
    [tabBarViewController didMoveToParentViewController:self];
    UIView *controllerView = tabBarViewController.view;
    id<UILayoutSupport> rootTopLayoutGuide = self.topLayoutGuide;
    id<UILayoutSupport> rootBottomLayoutGuide = self.bottomLayoutGuide;
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(rootTopLayoutGuide, rootBottomLayoutGuide, controllerView);
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[rootTopLayoutGuide]["@"controllerView][" @"rootBottomLayoutGuide]" options:0 metrics:nil views:viewsDictionary]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[controllerView]|" options:0 metrics:nil views:viewsDictionary]];
//    [controllerView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view];
//    [controllerView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
//    [controllerView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view];
//    [controllerView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.view withOffset:64];
}

- (void)setupBarButton {
//    LEBarButtonItem *item = [[LEBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_border_color_white_24dp"] andSelImage:[UIImage imageNamed:@"ic_border_color_white_24dp"] target:self action:@selector(click)];
    
    LEBarButtonItem *me = [[LEBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Guest_Filled-50"] andSelImage:[UIImage imageNamed:@"Guest_Filled-50"] target:self action:@selector(meClick)];
    UIBarButtonItem *placeholder = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];
    self.navigationItem.rightBarButtonItems = @[placeholder, me];
}

- (void)addBarButton {
    PaperButton *button = [PaperButton button];
    [button addTarget:self action:@selector(paperButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.tintColor = [UIColor whiteColor];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItem = barButton;
}

- (void)paperButtonClick:(PaperButton *)btn {
//  CYWeakObject(wself, self);
//    [LEMoreView showMenuWithData:@[@"发布租车信息", @"车辆管理", @"个人中心"] showInView:self.navigationController.view didSelectRow:^(NSInteger indexPath) {
//        if (indexPath == 0) {
//            [wself click];
//        } else if (indexPath == 1) {
//            
//        } else if (indexPath == 2) {
//            [wself meClick];
//        }
//        [btn buttonTouched];
//    }];
}

- (void)click {
    CYUserCarPublishTableVC *publish = [CYUserCarPublishTableVC le_loadFromStoryBoard:[CYJumpsTool mainSB]];
    publish.userType = self.userType;
    [self.navigationController pushViewController:publish animated:YES];
}

- (void)meClick {
//    CYPersonalDetailsVController *person = [CYPersonalDetailsVController le_loadFromStoryBoard:[CYJumpsTool mainSB]];
//    [self.navigationController pushViewController:person animated:YES];
    if (self.userType == CYUserTypePerson) {
        [CYJumpsTool jumpToUserInfo:self];
    } else {
        [CYJumpsTool jumpToCompanyInfo:self pushType:CYPushTypePush];
    }
}

- (void)loadUserStatus {
    [MBProgressHUD showMessage:nil toView:self.view];
    [CYCompanyUserTool getUserWithUseType:self.userType user:^(CYUser *user) {
        [MBProgressHUD hideHUDForView:self.view];
        if (user) {
            if (user.certificateExamineStatus.intValue != CYExamineStatusYES) {
                [CYMeHttpTool getUserStatusForSuccess:^(CYUser *user) {
                    if (user.certificateExamineStatus.intValue == CYExamineStatusYES) {
                        [self initTabBar];
                    } else {
                        [CYJumpsTool jumpToUserInfo:self];
                        [self.navigationController popViewControllerAnimated:NO];
                    }
                } failure:^(NSString *message, BOOL isLogin) {
                    if (!isLogin) {
                        [self.navigationController popViewControllerAnimated:NO];
                        [CYLoginHttpTool logoutWithController:self];
                    }
                    [MBProgressHUD showError:message];
                }];
            } else {
                [self initTabBar];
            }
        } else {
            [CYJumpsTool jumpToLogin:self];
        }
    } companyUser:^(CYCompanyUserModel *companyUser) {
        [MBProgressHUD hideHUDForView:self.view];
        if (companyUser) {
            if (companyUser.examineStatus.intValue != CYExamineStatusYES) {
                [CYMeHttpTool getCompanyUserStatusForSuccess:^(CYCompanyUserModel *user) {
                    if (user.examineStatus.intValue == CYExamineStatusYES) {
                        [self initTabBar];
                    } else {
                        [CYJumpsTool jumpToCompanyInfo:self pushType:CYPushTypePresent];
                        [self.navigationController popViewControllerAnimated:NO];
                    }
                } failure:^(NSString *message, NSError *error) {
                    if ([message equals:kCYNOLOGIN]) {
                        [CYCompanyUserTool logoutWithController:self];
                    } else if (error) {
                        [MBProgressHUD showError:@"服务器连接错误"];
                    }
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            } else {
                [self initTabBar];
            }
        } else {
            [CYJumpsTool jumpToCompanyLogin:self];
            [self.navigationController popViewControllerAnimated:NO];
        }
    }];
}

#pragma mark - delegate
- (UIViewController *)tabBarViewController: (MDTabBarViewController *)viewController viewControllerAtIndex:(NSUInteger)index {
    
    UIViewController *controller = (index == 0) ? self.leaseVC : self.dropCarVC;
    return controller;
}

@end
